package com.hw.ui.scanfconfig;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.hw.HWApp;
import com.hw.R;
import com.hw.base.activity.BaseActivity_noMVP;
import com.hw.base.util.Util;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScanfConfigActivity extends BaseActivity_noMVP {
    private String tag = "ScanfConfigActivity";
    @Bind(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvright)
    TextView tvright;
    @Bind(R.id.et_wll)
    EditText etWll;
    @Bind(R.id.et_wlh)
    EditText etWlh;
    @Bind(R.id.et_snl)
    EditText etSnl;
    @Bind(R.id.et_snh)
    EditText etSnh;
    @Bind(R.id.surebt)
    Button surebt;
    List<Integer> wls = new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanfconfig);
        ButterKnife.bind(this);
        tvTitle.setText("扫描配置");
        initdata();
    }

    private void initdata() {
        etWll.setText(HWApp.sSharedPreferences.getInt(Util.boxmixlen, 0) + "");
        etWlh.setText(HWApp.sSharedPreferences.getInt(Util.boxmaxlen, 0) + "");
        etSnl.setText(HWApp.sSharedPreferences.getInt(Util.casemixlen, 0) + "");
        etSnh.setText(HWApp.sSharedPreferences.getInt(Util.casemaxlen, 0) + "");
//        etWll.setText(HWApp.sSharedPreferences.getInt(Util.boxmixlen, 2) + "");
//        etWlh.setText(HWApp.sSharedPreferences.getInt(Util.boxmaxlen, 2) + "");
//        etSnl.setText(HWApp.sSharedPreferences.getInt(Util.casemixlen, 1) + "");
//        etSnh.setText(HWApp.sSharedPreferences.getInt(Util.casemaxlen, 1) + "");
    }

    private boolean contain() {
        wls.clear();
        for (int i = Integer.parseInt(etWll.getText().toString()); i <= Integer.parseInt(etWlh.getText().toString()); i++) {
            wls.add(i);
        }
        for (int i = Integer.parseInt(etSnl.getText().toString()); i <= Integer.parseInt(etSnh.getText().toString()); i++) {
            for (int j = 0; j < wls.size(); j++) {
                if (wls.get(j) == i) {
                    return true;
                }
            }
        }
        return false;
    }

    @OnClick({R.id.iv_title_left, R.id.surebt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.surebt:
                if (Integer.parseInt(etWll.getText().toString()) == 0 || Integer.parseInt(etSnl.getText().toString()) == 0) {
                    showMsg("条码长度不能为0");
                    return;
                }
                if (Integer.parseInt(etWll.getText().toString()) > Integer.parseInt(etWlh.getText().toString())) {
                    showMsg("盒码长度不能从大到下");
                    return;
                }
                if (Integer.parseInt(etSnl.getText().toString()) > Integer.parseInt(etSnh.getText().toString())) {
                    showMsg("箱码长度不能从大到下");
                    return;
                }
                if (contain()) {
                    showMsg("两种码长的范围不能有交集");
                    return;
                }
                HWApp.sEditor.putInt(Util.boxmixlen,Integer.parseInt(etWll.getText().toString()));
                HWApp.sEditor.putInt(Util.boxmaxlen,Integer.parseInt(etWlh.getText().toString()));
                HWApp.sEditor.putInt(Util.casemixlen,Integer.parseInt(etSnl.getText().toString()));
                HWApp.sEditor.putInt(Util.casemaxlen,Integer.parseInt(etSnh.getText().toString()));
                HWApp.sEditor.commit();
                showMsg("扫描配置完成");
                finish();
                break;
        }
    }
}
